package dao.hbm;

import java.util.ArrayList;

import org.hibernate.Session;
import metier.Candidature;
import metier.Stage;
import dao.DAOCandidature;
import dao.DAOStage;

public class DAOCandidatureHBM implements DAOCandidature 
{

	private	static DAOCandidature	instance = null;
	
	private	DAOCandidatureHBM()
	{}

	public static DAOCandidature getInstance()
	{
		if (instance==null)
		{
			instance = new DAOCandidatureHBM();
		}
		return instance;
	}

	@Override
	public void save(Candidature candidature) throws Exception
	{
		if (candidature.getNumCandidature()==-1)
		{
			Session	session = DAOHibernate.getInstance().connect();
	
			session.save(candidature);
			
			DAOHibernate.getInstance().close(session);
			
		}
		else
		{
			throw new Exception("Impossible d'enregistrer car l'objet est deja dans la base");
		}	
	}

	@Override
	public void saveOrUpdate(Candidature candidature) throws Exception
	{
		if (candidature.getNumCandidature()==-1)
			save(candidature);
		else 
		{

			Session	session = DAOHibernate.getInstance().connect();

			session.update(candidature);
		
			DAOHibernate.getInstance().close(session);
		}
	}

	@Override
	public void delete(Candidature candidature) throws Exception
	{
		if (candidature.getNumCandidature()!=-1)
		{
			Session	session = DAOHibernate.getInstance().connect();
			session.delete(candidature);
			DAOHibernate.getInstance().close(session);
		}
		else
		{
			throw new Exception("Suppression impossible car l'objet n'est pas dans la base");
		}
	}

	@Override
	public Candidature get(int numCandidature) throws Exception
	{
		Session	session = DAOHibernate.getInstance().connect();
		
		Candidature candidature = (Candidature) session.get(Candidature.class, numCandidature);
		
		if (candidature==null)
			throw new Exception("La candidature avec le code "+numCandidature+" est introuvable!");
		
		DAOHibernate.getInstance().close(session);
		
		return candidature;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Candidature> loadAll(int num_etu) throws Exception
	{
		Session	session = DAOHibernate.getInstance().connect();
		ArrayList<Candidature> result = new ArrayList<Candidature>();

		ArrayList<Integer> tab = (ArrayList<Integer>) session.createSQLQuery("SELECT num_cand FROM candidature WHERE num_etu=?").setInteger(0, num_etu).list();
		
		for (Integer i : tab)
		{
			Candidature c = (Candidature)session.get(Candidature.class, i.intValue());
			result.add(c);
		}
		
		DAOHibernate.getInstance().close(session);
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Candidature> loadAll() throws Exception {
		Session	session = DAOHibernate.getInstance().connect();
		ArrayList<Candidature> tab = new ArrayList<Candidature>();
		
		tab = (ArrayList<Candidature>)session.createQuery("FROM Candidature").list();
		
		DAOHibernate.getInstance().close(session);
		return tab;
	}

	@Override
	public ArrayList<Candidature> load(int ref_dep) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public boolean Existe(Candidature candidature) throws Exception
	{
		Session	session = DAOHibernate.getInstance().connect();
		@SuppressWarnings("unchecked")
		ArrayList<Candidature> cand = (ArrayList<Candidature>) session.createQuery("FROM Candidature WHERE numEtudiant= :numE AND numStage= :numS").
								setInteger("numE", candidature.getNumEtudiant()).setInteger("numS", candidature.getNumStage()).list();
		
		return !(cand.size() == 0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Candidature> loadFromStage(int ref_stage) throws Exception {
		Session	session = DAOHibernate.getInstance().connect();
		ArrayList<Candidature> tab = new ArrayList<Candidature>();
		
		tab = (ArrayList<Candidature>)session.createQuery("FROM Candidature WHERE numStage= :ref").setInteger("ref", ref_stage).list();
		
		DAOHibernate.getInstance().close(session);
		return tab;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Candidature> loadFromDep(int numDep) throws Exception {
		Session	session = DAOHibernate.getInstance().connect();
		DAOStage daoStage = DAOStageHBM.getInstance();
		ArrayList<Stage> stages = daoStage.loadFromDep(numDep);
		ArrayList<Candidature> tab = new ArrayList<Candidature>();
		
		for(Stage s : stages)
		{
			ArrayList<Candidature> cand = new ArrayList<Candidature>();
			cand = (ArrayList<Candidature>)session.createQuery("FROM Candidature WHERE numStage= :ref").setInteger("ref", s.getNumStage()).list();
			tab.addAll(cand);
		}
		
		DAOHibernate.getInstance().close(session);
		return tab;
	}
}
